package com.base.themvx.widget

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Typeface
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.StringRes
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import com.base.themvx.CL
import com.base.themvx.R

/**
 ******************(^_^)***********************<br>
 * Author: fee(QQ/WeiXin:1176610771)<br>
 * Date: 2023/8/30<br>
 * Time: 10:06<br>
 * <P>DESC:
 * 通用的入口条目信息控件
 * </p>
 * ******************(^_^)***********************
 */
class EntryInfosItemView(context: Context, attributeSet: AttributeSet?) :
    ConstraintLayout(context, attributeSet) {

    var ivEntryIcon: ImageView
    var tvEntryTitle: TextView
    var tvSubTitleRight: TextView
    var ivEntryHint: ImageView

    init {
        ivEntryIcon = ImageView(context)
        ivEntryIcon.id = View.generateViewId()
        addSubView(ivEntryIcon) {
            startToStart = CL.PARENT_ID
            topToTop = CL.PARENT_ID
            bottomToBottom = CL.PARENT_ID

        }

        tvEntryTitle = TextView(context)
        tvEntryTitle.id = View.generateViewId()
        addSubView(tvEntryTitle) {
            startToEnd = ivEntryIcon.id
            topToTop = CL.PARENT_ID
            bottomToBottom = CL.PARENT_ID
        }

        ivEntryHint = ImageView(context)
        ivEntryHint.id = View.generateViewId()
        addSubView(ivEntryHint) {
            endToEnd = CL.PARENT_ID
            topToTop = CL.PARENT_ID
            bottomToBottom = CL.PARENT_ID
        }

        tvSubTitleRight = TextView(context)
        tvSubTitleRight.id = View.generateViewId()
        addSubView(tvSubTitleRight) {
            topToTop = CL.PARENT_ID
            bottomToBottom = CL.PARENT_ID
            endToStart = ivEntryHint.id
        }

        initAttrs(context, attributeSet)
    }

    private fun initAttrs(context: Context, attributeSet: AttributeSet?) {
        attributeSet?.let { attr ->
            val attrCount = attr.attributeCount
            for (i in 0 until attrCount) {
                val attributeName = attr.getAttributeName(i)
                val attributeValue = attr.getAttributeValue(i)

//                AppLog.i("EntryInfosItemView", "--> attributeName = $attributeName,attributeValue = $attributeValue")

            }
            val typedArray: TypedArray =
                context.obtainStyledAttributes(attr,
                    R.styleable.EntryInfosItemView,)
            //理解 就算 xml中没有定义该属性，也是有值 如：3
            val indexEntryIcon = R.styleable.EntryInfosItemView_entryIcon //==3?
            val entryIcon = typedArray.getDrawable(indexEntryIcon)
            entryIcon?.let {
                ivEntryIcon.setImageDrawable(it)
            }
            val indexIconWidth = R.styleable.EntryInfosItemView_entryIconWidth //5
            val iconWidth =  typedArray.getDimensionPixelSize(indexIconWidth,0)
            val indexIconHeight = R.styleable.EntryInfosItemView_entryIconHeight
            val iconHeight =  typedArray.getDimensionPixelSize(indexIconHeight,0)
            if (iconWidth > 0 && iconHeight > 0) {
                ivEntryIcon.updateLayoutParams<CL> {
                    width = iconWidth
                    height = iconHeight
                }
            }
            //xml中直接写 "messge",也是可以获取到 文本的
            val indexEntryTitle = R.styleable.EntryInfosItemView_entryTitle
            val text = typedArray.getText(indexEntryTitle)
            tvEntryTitle.text = text
            val indexEntryTitleTextSize = R.styleable.EntryInfosItemView_entryTitleTextSize
            val titleTextSize = typedArray.getDimensionPixelSize(indexEntryTitleTextSize,0)
            if (titleTextSize > 0){
                tvEntryTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX,titleTextSize.toFloat())
            }
            val indexTitleTextColor = R.styleable.EntryInfosItemView_entryTitleTextColor
            val titleTextColor = typedArray.getColor(indexTitleTextColor, 0)
            if (titleTextColor != 0){
                tvEntryTitle.setTextColor(titleTextColor)
            }
            val titleMarginLeft = typedArray.getDimensionPixelSize(
                R.styleable.EntryInfosItemView_entryTitleMarginLeft,
                0
            )
            if (titleMarginLeft > 0){
                tvEntryTitle.updateLayoutParams<CL> {
                    marginStart = titleMarginLeft
                }
            }
            val indexTitleStyleBold = R.styleable.EntryInfosItemView_entryTitleStyleBold
            val isTitleStyleBold = typedArray.getBoolean(indexTitleStyleBold, false)
            if (isTitleStyleBold) {
                tvEntryTitle.typeface = Typeface.DEFAULT_BOLD
            }
            val indexHintIcon = R.styleable.EntryInfosItemView_entryHintIcon
            val entryHintIcon = typedArray.getDrawable(indexHintIcon)
            entryHintIcon?.let {
                ivEntryHint.setImageDrawable(it)
            }
            val indexHintIconWidth = R.styleable.EntryInfosItemView_entryHintIconWidth
            val hintIconWidth =  typedArray.getDimensionPixelSize(indexHintIconWidth,0)
            val indexHintIconHeight = R.styleable.EntryInfosItemView_entryHintIconHeight
            val hintIconHeight =  typedArray.getDimensionPixelSize(indexHintIconHeight,0)
            if (hintIconWidth > 0 && hintIconHeight > 0) {
                ivEntryHint.updateLayoutParams<CL> {
                    width = iconWidth
                    height = iconHeight
                }
            }
            val indexEntrySubTitle = R.styleable.EntryInfosItemView_entrySubTitle
            val subTitle = typedArray.getText(indexEntrySubTitle)
            tvSubTitleRight.text = subTitle
            val indexEntrySubTitleTextSize = R.styleable.EntryInfosItemView_entrySubTitleTextSize
            val subTitleTextSize = typedArray.getDimensionPixelSize(indexEntrySubTitleTextSize,0)
            if (subTitleTextSize > 0){
                tvSubTitleRight.setTextSize(TypedValue.COMPLEX_UNIT_PX,subTitleTextSize.toFloat())
            }
            val indexSubTitleTextColor = R.styleable.EntryInfosItemView_entrySubTitleTextColor
            val subTitleTextColor = typedArray.getColor(indexSubTitleTextColor, 0)
            if (subTitleTextColor != 0){
                tvSubTitleRight.setTextColor(subTitleTextColor)
            }
            val subTitleMarginRight = typedArray.getDimensionPixelSize(
                R.styleable.EntryInfosItemView_entrySubTitleMarginRight,
                0
            )
            if (subTitleMarginRight > 0){
                tvSubTitleRight.updateLayoutParams<CL> {
                    marginEnd = subTitleMarginRight
                }
            }
            val indexSubtitleStyleBold = R.styleable.EntryInfosItemView_entrySubTitleStyleBold
            val isSubtitleStyleBold = typedArray.getBoolean(indexSubtitleStyleBold,false)
            if (isSubtitleStyleBold) {
                tvSubTitleRight.typeface = Typeface.DEFAULT_BOLD
            }
            typedArray.recycle()
        }
    }

    var aExtraSubView: View? = null

    fun addSubView(subView: View, configs: CL.() -> Unit) {
        val lp = CL(CL.WRAP_CONTENT, CL.WRAP_CONTENT)
        lp.configs()
        addView(subView, lp)
    }

    fun setTitle(@StringRes textRes: Int){
        tvEntryTitle.setText(textRes)
    }

    fun setSubTitle(@StringRes textRes: Int){
        tvSubTitleRight.setText(textRes)
    }

}